/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 *
 */

import { Breadcrumb } from 'antd';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { routes } from '../../common/router';
import { homePage } from '../../config/base';
import menuData from '../../common/menu';

export default class BreadcrumbNav extends React.Component {
  public getBreadcrumb() {
    const path: string = location.pathname.slice(1);
    if (path.length === 0) {
      for (const item of routes) {
        if (item.path === '/') {
          return (
            <Breadcrumb.Item>
              <span>{item.name}</span>
            </Breadcrumb.Item>
          );
        }
      }
    }
    if (path.indexOf('/') === -1) {
      for (const item of routes) {
        if (item.path === '/' + path) {
          return (
            <Breadcrumb.Item>
              <span>{item.name}</span>
            </Breadcrumb.Item>
          );
        }
      }
    }
    const pathList: string[] = path.split('/');
    return pathList.map((itemPath: string, index: number) => {
      if (index > 0) {
        itemPath = pathList.slice(0, index).join('/') + '/' + itemPath;
      }
      for (const ele of menuData) {
        if (ele.path === '/' + itemPath) {
          return (
            <Breadcrumb.Item key={ele.name}>
              <span>{ele.name}</span>
            </Breadcrumb.Item>
          );
        }
      }

      for (const ele of routes) {
        const routerPath = ele.path;
        if (routerPath === '/' + itemPath) {
          return (
            <Breadcrumb.Item key={ele.name}>
              {index === pathList.length - 1 ? (
                <span>{ele.name}</span>
              ) : (
                <Link
                  replace={true}
                  style={{ textDecoration: 'none' }}
                  to={ele.path}
                >
                  {ele.name}
                </Link>
              )}
            </Breadcrumb.Item>
          );
        }
      }
      return null;
    });
  }

  public render() {
    return (
      <Breadcrumb style={{ margin: '16px 16px' }}>
        <Breadcrumb.Item>
          <Link style={{ textDecoration: 'none' }} to={homePage || '/'}>
            首页
          </Link>
        </Breadcrumb.Item>
        {this.getBreadcrumb()}
      </Breadcrumb>
    );
  }
}
